<?php

namespace app\admin\controller\system;

use core\basic\Controller;
use app\admin\model\system\EditModel;

class EditController extends Controller
{

    public function __construct()
    {
        $this->model = new EditModel();
    }

    public function index()
    {
        $site = $this->model->getList();
        //模板地址
        $path = ROOT_PATH . current($this->config('tpl_dir')) . "/" . $site->theme;
        $basepath = $path;
        if ($_GET) {
            $dir = get('dir');
            //文件夹遍历
            if ($dir) {
                $path = session('pdir') . "/" . $dir;
                if (!is_dir($path)) {
                    $path = substr($path, 0, strrpos($path, '/'));
                    if (!is_dir($path)) {
                        alert_back('参数错误！');
                    }
                }
                $this->assign('pdir', $path);
                $this->assign('dir', $dir);
            }
            //上一页 这里有个BUG,点击上一页以后刷新 会默认继续跳转上一页
            $pdir = get('pdir');
            if ($pdir) {
                $curpath = session('pdir');
                if ($curpath != $basepath) {
                    //上一级地址
                    $path = substr(session('pdir'), 0, strrpos(session('pdir'), '/'));
                    //如果上级目录是模板目录则无上一页值为空
                    $pdir = $path == $basepath ? '' : substr($path, strrpos($path, '/') + 1);
                    $this->assign('pdir', $pdir);
                    $this->assign('dir', $pdir);
                }
            }
        }
        $list = $this->paths_list($path);
        //存入上级目录
        session('pdir', $path);
        $this->assign('dirlist', $list['dir']);
        $this->assign('filelist', $list['file']);
        $this->assign('path', $path);
        $this->display("system/edit.html");
    }

    public function edit()
    {
        if ($_POST) {
            $file_path = post("filepath");
            if (!is_file($file_path)) {
                alert_back('该文件不存在！');
            }
            file_put_contents($file_path, stripslashes(htmlspecialchars_decode(post("fileinfo"))), LOCK_EX);
            alert_back('操作成功！',-1);
        }
        $file = get('file');
        if (!strpos($file, '.')) {
            $file .= ".html";
        }
        if ($file) {
            $file_path = session('pdir') . "/" . $file;

            if (!is_file($file_path)) {
                alert_back('文件不存在！' . $file_path);
            }
            $fileinfo = file_get_contents($file_path);
            $this->assign('fileinfo', $fileinfo);
            $this->assign('filepath', $file_path);
        }

        $this->display("system/editinfo.html");
    }

    public function del()
    {
        $file = get('file');
        if (!strpos($file, '.')) {
            $file .= ".html";
        }
        if ($file) {
            $file_path = session('pdir') . "/" . $file;
            if (!is_file($file_path)) {
                alert_back('文件不存在！');
            }
            if (is_file($file_path) && file_exists($file_path)) {
                if (unlink($file_path)) {
                    alert_back('文件删除成功！');
                }else{
                    alert_back("删除失败");
                }
            }
        }
    }

    public function add()
    {
        if ($_POST) {
            $filename = post("filename");
            if ($filename) {
                $ext = substr($filename, strrpos($filename, '.') + 1);
                if (in_array($ext, array('html', 'css', 'js'))) {
                    $file_path = session('pdir') . "/" . $filename;
                    if(file_exists($file_path)){
                        alert_back("该文件已存在");
                    }
                    file_put_contents($file_path, stripslashes(htmlspecialchars_decode(post("fileinfo"))), LOCK_EX);
                    alert_back('操作成功！',-1);
                }else{
                    error("文件后缀名异常",-1);
                }
            }

        }
        $this->display("system/editadd.html");
    }

// 目录下文件及文件夹列表
    function paths_list($path)
    {
        $list = array();
        if (!is_dir($path) || !$filename = scandir($path)) {
            return $list;
        }
        $files = count($filename);
        for ($i = 0; $i < $files; $i++) {
            $dir = $path . '/' . $filename[$i];
            if (is_file($dir)) {
                $ext = substr($filename[$i], strrpos($filename[$i], '.') + 1);

                if ($ext == "js" || $ext == "css" || $ext == "html") {
                    $list['file'][] = $filename[$i];
                }
            }
            if (is_dir($dir) && $filename[$i] != '.' && $filename[$i] != '..') {
                $list['dir'][] = $filename[$i];
            }
        }
        return $list;

    }

    public function checkFileName($file)
    {
        if (strpos($file, '../') !== false || strpos($file, '..\\') !== false) return true;
        return false;
    }
}